package com.weavernorth.action.application;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * 判断时间是否存在重复
 */
public class CheckTimeRepeatAllAction implements Action {
    private static Log log = LogFactory.getLog(CheckTimeRepeatAllAction.class);

    @Override
    public String execute(RequestInfo requestInfo) {
        //请求id
        String strRequestid = requestInfo.getRequestid();
        //表名
        //String strTable = requestInfo.getRequestManager().getBillTableName();

        RecordSet rsTable=new RecordSet();
        String strTable ="";
        rsTable.execute("select tablename  from workflow_bill where id = (select formid from workflow_base where id=(select workflowid from workflow_requestbase  where requestid='"+strRequestid+"') and isbill=1)");
        if(rsTable.next()){
            strTable=rsTable.getString("tablename");
        }
        //数据库操作
        RecordSet rs = new RecordSet();
        RecordSet rsLeave = new RecordSet();
        //存放所有的开始日期时间list
        List<String> listBegin = new ArrayList<String>();
        //存放所有的结束日期时间list
        List<String> listEnd = new ArrayList<String>();
        //获取当前流程所有明细表的申请人
        String sql = "select fmd.detail_resourceId  from " + strTable + "  fm," + strTable + "_dt1 fmd where fm.id=fmd.mainid and  fm.requestid=? group by fmd.detail_resourceId";
        log.info("得到当前流程的申请人:" + sql + ";requestid:" + strRequestid);
        rs.executeQuery(sql, strRequestid);
        while (rs.next()) {
            //申请人
            String strUserid = Util.null2String(rs.getString("detail_resourceId"));
            //姓名
            String strLastName = "";
            //当前申请人的所有请假记录
            String strLeaveSql = "select hr.lastname ,fmd.detail_fromDate,fmd.detail_fromTime,fmd.detail_toDate ,fmd.detail_toTime" +
                    " from "+strTable+" fm ," + strTable + "_dt1 fmd,hrmresource hr,workflow_requestbase wr where " +
                    " fm.requestid=wr.requestid and fm.id=fmd.mainid  and " +
                    "hr.id=fmd.detail_resourceId and (wr.currentnodetype!='0' or wr.requestid=?) and   fmd.detail_resourceId=?";
          log.info("得到所有的日期sql:"+strLeaveSql+",strUserid:"+strUserid);
            rsLeave.executeQuery(strLeaveSql, strRequestid,strUserid);
            while (rsLeave.next()) {
                //开始日期时间
                String strBegin = Util.null2String(rsLeave.getString("detail_fromDate")) + Util.null2String(rsLeave.getString("detail_fromTime"));
               log.info("开始日期时间:"+strBegin);
                listBegin.add(strBegin);
                //结束日期时间
                String strEnd = Util.null2String(rsLeave.getString("detail_toDate")) + Util.null2String(rsLeave.getString("detail_toTime"));
                log.info("结束日期时间:"+strEnd);
                listEnd.add(strEnd);
                strLastName = Util.null2String(rsLeave.getString("lastname"));
            }
            //对比list是否存在重复
            boolean bolFlag = compareTime(listBegin, listEnd);
            log.info("重复判断返回结果:"+bolFlag);
            //存在重复
            if (!bolFlag) {
                requestInfo.getRequestManager().setMessagecontent("您申请的流程中,姓名为:" + strLastName + "的用户存在重复的时间");
                return FAILURE_AND_CONTINUE;

            }


        }


        return SUCCESS;
    }

    /**
     * 对比两个list的时间是否存在重复
     *
     * @param listBegin
     * @param listEnd
     * @return
     */
    public boolean compareTime(List<String> listBegin, List<String> listEnd) {
        //返回值
        boolean bolFlag = true;
        //升序排序
        Collections.sort(listBegin);
        Collections.sort(listEnd);
        //判断是否存在重叠
        if (listBegin.size() > 1) {
            //开始日期时间的下一行数据是否小于结束日期时间的上一行,小于则重叠
            for (int i = 1; i < listBegin.size(); i++) {
                //开始日期时间
                String strBegin = listBegin.get(i);
                //结束时间日期
                String strEnd = listEnd.get(i - 1);
                if (strBegin.compareTo(strEnd) < 0) {
                    bolFlag = false;
                }
            }
        }
        return bolFlag;
    }
}